<template>
  <div id="home" class="login">
    <div class="login_box">
      <div class="top">
        <span>登录</span>
      </div>
      <el-form :model="ruleForm" status-icon ref="ruleForm">
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入账号">
            <template #prefix>
              <i class="el-input__icon el-icon-user"></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码">
            <template #prefix>
              <i class="el-input__icon el-icon-lock"></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox label="Remember me" :v-model="remember"></el-checkbox>
        </el-form-item>
        <div class="btn_box">
          <el-button class="login_btn" type="primary" @click="onsubmit('ruleForm')">登&nbsp;录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {getLogin} from "../../api/axios/loginss";

export default {
  name: 'Login',
  data() {
    return {
      remember:false,//记住密码
      ruleForm: {
        username: '1234567890',
        password: '123456'
      }
    }
  },
  methods: {
    //登录
    onsubmit(Form) {
      this.$refs[Form].validate((valid) => {
        if (valid) {
          //let _this = this;
          let par = {
            username: this.ruleForm.username,
            password: this.ruleForm.password
          }
         getLogin(par).then(res => {
            if (res.data.code === 200) {
              console.log(res.data.data);
              sessionStorage.setItem('userNickname', res.data.data.nickname);
              sessionStorage.setItem('token', res.data.data.token);
              //this.$store.commit('getUserNickname',res.data.data.nickname)
              this.$router.push('/home')
            }
            else {
              this.$message.error(res.data.message);
              this.ruleForm = {}
            }
          }).catch(err => {
            throw err
          })
        }else  return this.$message.error('错误！！！！')
      })
    },
  }
}
</script>
<style scoped>
.login {
  height: 100%;
  display: flex;
  justify-content: center;
}

.login_box {
  position: absolute;
  padding: 50px 35px;
  width: 95%;
  max-width: 375px;
  transform: translate(0, 35%);
  background-color: whitesmoke;
  box-sizing: border-box;
}

.top {
  font-size: 30px;
  text-align: center;
  margin-bottom: 40px;
  font-family: fangsong;
}

.btn_box {
  display: flex;
  justify-content: center;
}

.login_btn {
  width: 100%;
  margin-top: -15px;

}
</style>
